<?php
/**
 * Created by PhpStorm.
 * User: ownfi
 * Date: 2017-04-09
 * Time: 11:20
 */

namespace App\Services\Impl;


use App\Common\Constant\Params;
use App\Models\Master;
use App\Models\MasterFund;
use App\Models\MasterTradeLog;
use App\Models\MasterWallet;
use App\Models\Order;
use App\Models\OrderRefund;
use App\Services\IMasterLogService;
use App\Services\ISystemService;
use App\Services\IWalletService;
use DB;
use Log;

class MasterLogServiceImpl implements IMasterLogService
{
    private $systemService;

    /**
     * PayServiceImpl constructor.
     */
    public function __construct(ISystemService $systemService)
    {
        $this->systemService = $systemService;
    }

    public function PayAssureFundLog(MasterFund $fundOrder,$pay_method)
    {
        DB::beginTransaction();
        try{
            //生成交易流水号
            $trade_number = $this->systemService->genTradeNumber();

            $master = Master::find($fundOrder->master_id);
            $wallet = MasterWallet::where('master_id',$fundOrder->master_id)->lockForUpdate()->first();

            //是否是用余额支付的
            $pay_by_balance = $pay_method == Params::PAY_METHOD_BALANCE;
            //支付来源
            $source = [
                Params::PAY_METHOD_WEIXIN => Params::TRADE_SOURCE_WEIXIN ,
                Params::PAY_METHOD_ALIPAY => Params::TRADE_SOURCE_ALIPAY ,
                Params::PAY_METHOD_UNIONPAY => Params::TRADE_SOURCE_UNIONPAY ,
                Params::PAY_METHOD_BALANCE => Params::TRADE_SOURCE_BALANCE ,
            ][$pay_method];

            $ret = MasterTradeLog::create([
                'trade_number'=>$trade_number,
                'master_id'=>$fundOrder->master_id,
                'master_name'=>$master->real_name,
                'direction'=>Params::TRADE_DIRECTION_OUT,
                'amount'=>$fundOrder->amount,
                'type'=>Params::TRADE_TYPE_ASSURE,
                'source'=>$source,
                'balance'=>$pay_by_balance ? $wallet->balance - $fundOrder->amount : $wallet->balance,
                'assure_fund'=>$wallet->assure_fund + $fundOrder->amount,
                'order_number'=>$fundOrder->id,
                'order_serial_number'=>$fundOrder->order_number,
                'ip'=>getip(),
                'status'=>Params::TRADE_STATUS_WAITING,
            ]);

            if ($ret){
                DB::commit();
            }else{
                Log::error(sprintf('%s/%s::%s[%d] %s',__NAMESPACE__,__CLASS__,__FUNCTION__,__LINE__-2,'PayAssureFundLog fail'));
                DB::rollBack();
            }

            return $ret;
        }catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }
    }

    public function orderMoneyArriveLog(Order $order)
    {
        DB::beginTransaction();
        try{
            $wallet = MasterWallet::where('master_id',$order->master_id)->lockForUpdate()->first();

            //加上补款金额
            $amount = $order->master_money + $order->replenish_total;

            $model = MasterTradeLog::where(['type'=>Params::TRADE_TYPE_ORDER,'order_number'=>$order->id])->first();
            if ($model){
                $ret = $model->update([
                    'balance'=>$wallet->balance + $amount,
                    'assure_fund'=>$wallet->assure_fund,
                    'ip'=>getip(),
                ]) ? $model : false;
            }else{
                //生成交易流水号
                $trade_number = $this->systemService->genTradeNumber();

                $master = Master::find($order->master_id);

                $ret = MasterTradeLog::create([
                    'trade_number'=>$trade_number,
                    'master_id'=>$order->master_id,
                    'master_name'=>$master->real_name,
                    'direction'=>Params::TRADE_DIRECTION_IN,
                    'amount'=>$amount,
                    'type'=>Params::TRADE_TYPE_ORDER,
                    'source'=>Params::TRADE_SOURCE_SYSTEM,
                    'balance'=>$wallet->balance + $amount,
                    'assure_fund'=>$wallet->assure_fund,
                    'order_number'=>$order->id,
                    'order_serial_number'=>$order->order_number,
                    'ip'=>getip(),
                    'status'=>Params::TRADE_STATUS_WAITING,
                ]);
            }

            if ($ret){
                DB::commit();
            }else{
                Log::error(sprintf('%s/%s::%s[%d] %s',__NAMESPACE__,__CLASS__,__FUNCTION__,__LINE__-2,'orderMoneyArriveLog fail'));
                DB::rollBack();
            }

            return $ret;
        }catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }
    }

    public function masterPunishLog(Order $order,$amount)
    {
        DB::beginTransaction();
        try{
            //生成交易流水号
            $trade_number = $this->systemService->genTradeNumber();

            $master = Master::find($order->master_id);

            $wallet = MasterWallet::where('master_id',$order->master_id)->lockForUpdate()->first();

            $amount = floatval($amount);

            $ret = MasterTradeLog::create([
                'trade_number'=>$trade_number,
                'master_id'=>$order->master_id,
                'master_name'=>$master->real_name,
                'direction'=>Params::TRADE_DIRECTION_OUT,
                'amount'=>$amount,
                'type'=>Params::TRADE_TYPE_PUNISH,
                'source'=>Params::TRADE_SOURCE_BALANCE,
                'balance'=>$wallet->balance - $amount,
                'assure_fund'=>$wallet->assure_fund,
                'order_number'=>$order->id,
                'order_serial_number'=>$order->order_number,
                'ip'=>getip(),
                'status'=>Params::TRADE_STATUS_WAITING,
            ]);

            if ($ret){
                DB::commit();
            }else{
                Log::error(sprintf('%s/%s::%s[%d] %s',__NAMESPACE__,__CLASS__,__FUNCTION__,__LINE__-2,'masterPunishLog fail'));
                DB::rollBack();
            }

            return $ret;
        }catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }
    }

    public function orderRefundPayLog(OrderRefund $refund, $amount)
    {
        DB::beginTransaction();
        try{
            //生成交易流水号
            $trade_number = $this->systemService->genTradeNumber();

            $master = Master::find($refund->order->master_id);

            $wallet = MasterWallet::where('master_id',$refund->order->master_id)->lockForUpdate()->first();

            $amount = floatval($amount);

            $ret = MasterTradeLog::create([
                'trade_number'=>$trade_number,
                'master_id'=>$refund->order->master_id,
                'master_name'=>$master->real_name,
                'direction'=>Params::TRADE_DIRECTION_IN,
                'amount'=>$amount,
                'type'=>Params::TRADE_TYPE_REFUND,
                'source'=>Params::TRADE_SOURCE_SYSTEM,
                'balance'=>$wallet->balance + $amount,
                'assure_fund'=>$wallet->assure_fund,
                'order_number'=>$refund->id,
                'order_serial_number'=>$refund->order_number,
                'ip'=>getip(),
                'status'=>Params::TRADE_STATUS_WAITING,
            ]);

            if ($ret){
                DB::commit();
            }else{
                Log::error(sprintf('%s/%s::%s[%d] %s',__NAMESPACE__,__CLASS__,__FUNCTION__,__LINE__-2,'orderRefundPayLog fail'));
                DB::rollBack();
            }

            return $ret;
        }catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }
    }

}